//
//  LGChangePatientPhoneView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/8/28.
//

#import "LGChangePatientPhoneView.h"

@interface LGChangePatientPhoneView()

/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UITextField *phoneTF;
/**   */
@property (nonatomic , strong)UILabel *tipLabel;
/**   */
@property (nonatomic , strong)UIView *lineView_H;
/**   */
@property (nonatomic , strong)UIView *lineView_V;
/**   */
@property (nonatomic , strong)UIButton *cancelBtn;
/**   */
@property (nonatomic , strong)UIButton *sureBtn;
@end

@implementation LGChangePatientPhoneView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
        [self showAnimation];
    }
    return self;
}

-(void)showAnimation{
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#0D0D0D" alpha:0.7];
        self.baseView.transform = CGAffineTransformScale(self.baseView.transform, 1000, 1000);
    }];
}

-(void)removeAnimation{
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#0D0D0D" alpha:0];
        self.baseView.transform = CGAffineTransformScale(self.baseView.transform, 0.001, 0.001);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)sureButtonTouchAction{
    if([self.phoneTF.text stringByReplacingOccurrencesOfString:@" " withString:@""].length < 11){
        [TooltipView showMessage:@"请输入完整手机号" offset:0];
    }else{
        if(self.changePatientPhone){
            self.changePatientPhone(self.phoneTF.text);
        }
        [self removeAnimation];
    }
}

-(void)textFieldEditingChanged:(UITextField *)textField{
    NSString *content = textField.text;
    self.phoneTF.text = (content.length>11)?[content substringToIndex:11]:content;
    self.sureBtn.selected = (content.length == 11)?YES:NO;
    self.sureBtn.userInteractionEnabled = (content.length == 11)?YES:NO;
}

#pragma mark ====> 创建控件
-(void)createSubView{
    self.backgroundColor = [UIColor colorWithString:@"#0D0D0D" alpha:0];
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.phoneTF];
    [self.baseView addSubview:self.tipLabel];
    [self.baseView addSubview:self.lineView_H];
    [self.baseView addSubview:self.lineView_V];
    [self.baseView addSubview:self.cancelBtn];
    [self.baseView addSubview:self.sureBtn];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(self);
        make.width.equalTo(@(viewPix(320)));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView).offset(viewPix(27));
        make.left.equalTo(self.baseView).offset(viewPix(25));
        make.right.equalTo(self.baseView).offset(-viewPix(25));
    }];
    [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(13));
        make.left.right.equalTo(self.titleLabel);
        make.height.equalTo(@(viewPix(44)));
    }];
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneTF.mas_bottom).offset(viewPix(12));
        make.left.right.equalTo(self.titleLabel);
        make.bottom.equalTo(self.lineView_H.mas_top).offset(-viewPix(30));
    }];
    
    [self.lineView_H mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.baseView);
        make.bottom.equalTo(self.baseView).offset(-viewPix(55));
        make.height.equalTo(@(1));
    }];
    
    [self.lineView_V mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView_H.mas_bottom);
        make.bottom.equalTo(self.baseView);
        make.centerX.equalTo(self.baseView);
        make.width.equalTo(@(1));
    }];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView_H.mas_bottom);
        make.right.equalTo(self.lineView_V.mas_left);
        make.left.bottom.equalTo(self.baseView);
    }];
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lineView_V.mas_right);
        make.right.bottom.equalTo(self.baseView);
        make.top.equalTo(self.cancelBtn);
    }];
}
-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]initWithFrame:CGRectMake((Screen_W-viewPix(320))/2.0, (Screen_H-viewPix(224))/2.0, viewPix(320), viewPix(224))];
        _baseView.transform = CGAffineTransformScale(_baseView.transform, 0.001, 0.001);
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 8;
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"修改号码" colorString:@"#333333" textFont:LGFontWeight(17, UIFontWeightSemibold) textAlignment:NSTextAlignmentCenter lines:3];
    }
    return _titleLabel;
}

-(UITextField *)phoneTF{
    if(!_phoneTF){
        _phoneTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(25), viewPix(64), viewPix(272), viewPix(44))];
        _phoneTF.backgroundColor = [UIColor colorWithString:@"#F7F7F7"];
        _phoneTF.tintColor = [UIColor colorWithString:@"#333333"];
        _phoneTF.textColor = [UIColor colorWithString:@"#333333"];
        _phoneTF.textAlignment = NSTextAlignmentLeft;
        _phoneTF.keyboardType = UIKeyboardTypeNumberPad;
        _phoneTF.font = LGFontWeight(18, UIFontWeightSemibold);
        _phoneTF.attributedPlaceholder = [@"请输入患者手机号" attributeString:@{NSFontAttributeName:LGFont(14)} range:NSMakeRange(0, 8)];
        _phoneTF.cornerRidus = 8;
        _phoneTF.leftViewMode = UITextFieldViewModeAlways;
        _phoneTF.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewPix(8), viewPix(44))];
        [_phoneTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _phoneTF;
}

-(UILabel *)tipLabel{
    if(!_tipLabel){
        _tipLabel = [UILabel lableWithText:@"修改后患者档案将同步修改" colorString:@"#666666" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _tipLabel;
}

-(UIView *)lineView_H{
    if(!_lineView_H){
        _lineView_H = [[UIView alloc]init];
        _lineView_H.backgroundColor = [UIColor colorWithString:@"#DDDDDD"];
    }
    return _lineView_H;
}

-(UIView *)lineView_V{
    if(!_lineView_V){
        _lineView_V = [[UIView alloc]init];
        _lineView_V.backgroundColor = [UIColor colorWithString:@"#DDDDDD"];
    }
    return _lineView_V;
}

-(UIButton *)cancelBtn{
    if(!_cancelBtn){
        _cancelBtn = [UIButton buttonWithTitle:@"取消" titleFont:LGFont(17) textColor:@"#333333" imageName:@""];
        [_cancelBtn addTarget:self action:@selector(removeAnimation) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

-(UIButton *)sureBtn{
    if(!_sureBtn){
        _sureBtn = [UIButton buttonWithTitle:@"确定" titleFont:LGFontWeight(17, UIFontWeightMedium) normalColor:@"#A0CDFF" selectColor:@"#429BFF"];
        [_sureBtn addTarget:self action:@selector(sureButtonTouchAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}


@end
